import config from '../static/js/env.js'

class Request {
	// 接口域名
	URL = '';
	
	constructor(url){
		url ? this.URL = url : this.URL = config.API_URL
	}
	
	get(url, data) {
		return this.request('GET', url, data)
	}
	
	post(url, data) {
		return this.request('POST', url, data)
	}
	
	request(method, url, data) {
		return new Promise((resolve, reject) => {
			uni.request({
				method,
				url: this.URL + url,
				data,
				header: {
					'token': uni.getStorageSync('token') || ''
				},
				success({data: res}) {
					if (res.code == 1) resolve(res)
					else reject(res)
				},
				fail: () => {},
			})
		})
	}
}

const request = new Request()

export default request

export {
	Request
}